home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 02 / netbios / common.c < prev    next >
C/C++ Source or Header  |  1992-02-29  |  6KB  |  257 lines

  1. /***************************************************************************
  2.  *  COMMON.C                                   *
  3.  *    This file contains common functions                   *
  4.  *                                       *
  5.  *      ClearNcb () - Clear out a NCB                       *
  6.  *      NetBiosRequest() - Submit a NetBIOS call. OS dependent       *
  7.  *                                       *
  8.  *  History:    Alok Sinha  October, 1991    Created               *
  9.  *                                       *
  10.  ***************************************************************************/
  11.  
  12. // Include files
  13. #include <ncb.h>
  14. #include <common.h>
  15.  
  16. #include <stdio.h>
  17. #include <memory.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <process.h>
  21.  
  22. /*
  23.  * For windows, we will call the NetBiosCall API from a assembly
  24.  * routine. The prototype is defined below.
  25.  */
  26. #if defined (WIN)
  27. extern unsigned int far pascal NetBios ( char far *NCB);
  28. #endif
  29.  
  30. #if defined (DOS)
  31. #include <dos.h>
  32. #endif
  33.  
  34. /*
  35.  * Global Handle for OS/2. Designed for simplicity.
  36.  */
  37. #if defined (OS2)
  38. unsigned short far usNetHandle;
  39. unsigned  short NetBiosExit(  );
  40. #endif
  41.  
  42. /*
  43.  * FUNCTION    :: ClearNcb()
  44.  * PARAMETERS  ::
  45.  *     [in]  Ncb      :- A NCB structure pointer which will be set to 0.
  46.  * RETURN VALUE:
  47.  *        NONE.
  48.  */
  49.  
  50. void           ClearNcb       ( NCB far *pNcb)
  51. {
  52.     /* Fill NCB with 0x00   */
  53.     memset ( (void  *) pNcb, 0x00, sizeof (NCB) );
  54. }
  55.  
  56. /*
  57.  * FUNCTION    :: NetBiosRequest()
  58.  * PARAMETERS  ::
  59.  *     [in]  Ncb     :- A NCB structure pointer which will be submitted
  60.  *
  61.  * RETURN VALUE:
  62.  *        Return value from submitting NCB.
  63.  */
  64.  
  65. unsigned char NetBiosRequest (NCB far *pNcb)
  66. {
  67.  
  68. #if defined( DOS )
  69.  
  70.     union  REGS InRegs, OutRegs;
  71.     struct SREGS SegRegs;
  72.  
  73.     /* set ES:BX to point to the NCB */
  74.  
  75.     InRegs.x.bx = FP_OFF(pNcb);
  76.     SegRegs.es    = FP_SEG(pNcb);
  77.  
  78.     /* Make the Call    */
  79.     return (unsigned char) int86x(NETBIOS_CALL, &InRegs, &OutRegs, &SegRegs);
  80.  
  81. #elif defined( OS2 )
  82.  
  83.     return (unsigned char ) NetBiosSubmit ( usNetHandle, // Global net handle
  84.                                             0,           // Single NCB. Do not retry on error
  85.                                             pNcb);
  86.  
  87.  
  88. #elif defined( WIN )
  89.     return NetBios ( (char far *)pNcb);
  90. #endif /* OS dependent calls    */
  91.  
  92. }
  93.  
  94. #if defined(OS2)
  95.  
  96. /*
  97.  * FUNCTION    :: NetBiosInit()
  98.  * PARAMETERS  ::
  99.  *     [in]  pchNetBiosName    :- A char far pointer to logical NetBIOS name
  100.  *                   NULL means first netbios driver.
  101.  *     [in]  usOption        :- Mode of operation.
  102.  *                   NB_EXCLUSIVE, NB_PRIVILEGE, NB_REGULAR
  103.  *                   See "Microsoft Lan Manager Programmer's
  104.  *                   Reference" for detail.
  105.  *     [out] usNetHandle (Global):- It is set to a correct handle, if
  106.  *                   return value is NO_ERROR
  107.  *
  108.  * RETURN VALUE:
  109.  *        NO_ERROR if call was successful. else a OS/2 error.
  110.  *
  111.  * COMMENT:
  112.  *        In this sample program, we use a simple global handle
  113.  *        for storing the handle returned by NetBiosOpen(). In fact,
  114.  *        the following code will not even guard against multiple
  115.  *        calls to NetBiosInit and over-riding "old" handle if one
  116.  *        exists already. One should carefully modify these routines
  117.  *        if these were to be used for business applications.
  118.  */
  119.  
  120. unsigned  short NetBiosInit( char far      *pchNetBiosName,
  121.                unsigned short usOption
  122.              )
  123. {
  124.     /* Clear the global handle */
  125.     usNetHandle = 0;
  126.  
  127.     return NetBiosOpen ( pchNetBiosName,    // driver name e.g. "net1"
  128.              (char far *)0,        // reserved
  129.              usOption,
  130.              &usNetHandle
  131.             );
  132.  
  133. }
  134.  
  135. /*
  136.  * FUNCTION    :: NetBiosExit()
  137.  * PARAMETERS  ::
  138.  *           NONE
  139.  * RETURN VALUE:
  140.  *        NO_ERROR if call was successful. else a OS/2 error.
  141.  */
  142.  
  143. unsigned  short NetBiosExit(  )
  144. {
  145.  
  146.     return NetBiosClose ( usNetHandle,
  147.               (unsigned short) 0 // reserved
  148.             );
  149.  
  150. }
  151.  
  152. #endif /* OS/2 */
  153.  
  154. /*
  155.  * FUNCTION    :: CopyToBuffer()
  156.  * PARAMETERS  ::
  157.  *     [in/out] dest    :- char pointer to a buffer containing a NetBIOS name.
  158.  *     [in    ] src    :- char pointer to a buffer containing a name.
  159.  *
  160.  * RETURN VALUE:
  161.  *        NONE.
  162.  * Comment
  163.  */
  164.  
  165. void CopyToBuffer ( char *pchDest , char *pchSrc)
  166. {
  167.     register count;
  168.  
  169.     /* Check for null pointer */
  170.     if ((!pchDest) || ( ! pchSrc))
  171.         return ;
  172.  
  173.     /* set the name field with nulls */
  174.     memset ( pchDest, 0x20, NCBNAMSZ);
  175.  
  176.     /* copy from source to destination */
  177.     count =  NCBNAMSZ;
  178.     while ((*pchSrc) && ( count))
  179.     {
  180.         *pchDest++ = *pchSrc++;
  181.         count--;
  182.     }
  183.     return;
  184. }
  185.  
  186. /*
  187.  * FUNCTION    :: NetCleanUp()
  188.  * PARAMETERS  ::
  189.  *          [in]    Result: The result to be exited with to system.
  190.  * RETURN VALUE:
  191.  *        NO_ERROR if call was successful. else a OS/2 error.
  192.  *
  193.  * Comment:
  194.  *        Primarily needed for closing NetBios Handle for OS/2.
  195.  */
  196.  
  197. int NetCleanUp ( int Result)
  198. {
  199.  
  200. #ifdef OS2
  201.     unsigned short usRc;
  202.  
  203.     if ((usRc = NetBiosExit () )!= NO_ERROR)
  204.         printf("NetBiosExit Error [%d]\n", usRc);
  205. #endif
  206.  
  207.     return Result;
  208. }
  209.  
  210. /*
  211.  * FUNCTION    :: NetInit()
  212.  * PARAMETERS  ::
  213.  *          [in]    ucLana: The LAN Adaptor number
  214.  * RETURN VALUE:
  215.  *        NO_ERROR if call was successful. Else exit to system
  216.  *
  217.  * Comment:
  218.  *        Primarily needed for opening NetBios for OS/2.
  219.  *        The algorithm for determining netname (e.g. "net1")
  220.  *        works on Microsoft Lan Manager systems and not
  221.  *        guaranteed to work on other systems. One
  222.  *        should call NetBiosEnum() to determine the
  223.  *        correct netname (See "Microsoft
  224.  *        Lan Manager: Programmer's Reference" for detail)
  225.  */
  226.  
  227. int NetInit ( unsigned char ucLana)
  228. {
  229. #ifdef OS2
  230.  
  231.     char chNetName [ 20 ];
  232.     unsigned short usRc;
  233.  
  234.     if (ucLana==0)
  235.     memcpy(chNetName, "net1", sizeof("net1"));
  236.     else if (ucLana==1)
  237.     memcpy(chNetName, "net2", sizeof("net2"));
  238.     else
  239.     {
  240.     printf("Use NetBiosEnum to determine the correct Net Name\n");
  241.     exit (1);
  242.     }
  243.  
  244.     usRc = NetBiosInit( chNetName,  // Net Name
  245.             1        // regular open
  246.               );
  247.     if (usRc)
  248.     {
  249.     printf("NetBiosInit Error:: RC [%d]\n", usRc);
  250.     exit (1);
  251.     }
  252.  
  253. #endif
  254.     return 0;
  255.  
  256. }
  257.